import { viteMockServe } from 'vite-plugin-mock';

const createUserList = () => {
   return [
      {
         id: '@guid',
         uesrname: 'admin',
         password: '123456',
         lasttime: '@datetime',
         city: '@city',
         token: 'admin-token',
         desc: '@csentence(7, 15)',
         avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
         roles: ['平台管理员'],
         routes: [],
      },
      {
         id: '@guid',
         uesrname: 'system',
         password: '123456',
         token: 'system-token',
         desc: '@csentence(7, 15)',
         avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
         roles: ['系统管理员'],
         routes: [],
      },
      {
         id: '@guid',
         uesrname: 'user',
         password: '123456',
         token: 'user-token',
         desc: '@csentence(7, 15)',
         avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
         roles: ['普通用户'],
         routes: [],
      },
   ];
};

// 导出
export default [
   {
      url: '/mock/dev/login',
      method: 'post',
      response: ({ body }) => {
         // 获取请求携带过来的用户名和密码
         const { username, password } = body;
         // 获取用户列表，判断是否存在该用户
         const checkUser = createUserList().find(
            (item) => item.uesrname === username && item.password === password
         );
         // 用户名或密码错误
         if (!checkUser) {
            return {
               code: 400,
               msg: '用户名或密码错误',
               data: null,
            };
         }
         // 登录成功
         const { token } = checkUser;
         return {
            code: 200,
            msg: 'success',
            data: {
               token,
            },
         };
      },
   },
   {
      url: '/mock/dev/userInfo',
      method: 'get',
      response: (request) => {
         // 获取请求头携带的 token
         const { token } = request.headers;
         // 获取用户列表，判断是否存在该用户
         const checkUser = createUserList().find((item) => item.token == token);
         // 无用户信息
         if (!checkUser) {
            return {
               code: 201,
               msg: '获取用户信息失败',
               data: null,
            };
         }
         // 有用户信息
         return {
            code: 200,
            msg: 'success',
            data: {
               checkUser,
            },
         };
      },
   },
];
